<?php
include_once (SERVER_ROOT_PATH . '/Inc/Lib/Openapi/Openapi_kaixin.php');

class ml_model_openapiKaixin extends Lib_openapi_abstract {
    public $oauth;
    
    function __construct() { 
        $this->oauth = new KXOAuth( OPENAPI_KAIXIN_APP_KEY, OPENAPI_KAIXIN_APP_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret'] ); 
    }
    
    /*
     * 检测返回code
     */
    public function check_auth($code) {
        
        $response = $this->oauth->getAccessToken( $code );
        $a = array(
            'httpcode' => $this->oauth->http_code,
            'response' => $response,
        ); 
        
        $session = ml_controller::getSession ();
        $session->setVal ( 'access_token', $response ['oauth_token'] );
        $session->setVal ( 'token_secret', $response ['oauth_token_secret'] );
/*        //kaixin的uid
        $session->setVal ( '3rd_id', $token ['uid'] );      ??
        $session->getVal ( 'access_token' );
        */
        $session->save ();
        
        return array(
            'httpcode' => $this->oauth->http_code,
            'response' => $response,
        ); 
        
        
    }
    
    /*
     * 获取授权用链接
     */
    public function get_auth_url() {
                
        /* Build KXOAuth object with client credentials. */
        $connection = new KXClient(OPENAPI_KAIXIN_APP_KEY, OPENAPI_KAIXIN_APP_SECRET);
         
        /* Get temporary credentials. */
        $request_token_ret = $connection->getRequestToken(OPENAPI_CALLBACK_URL.'?type=kaixin','create_records create_album user_photo friends_photo upload_photo');
        
        $request_token = $request_token_ret['response'];
        $http_code = $request_token_ret['httpcode'];
        
        
        /* Save temporary credentials to session. */
        $_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
        $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
         
        
        /* Build authorize URL and redirect user to Twitter. */
        $url = $connection->getAuthorizeURL($request_token);
        
        if($url['httpcode'] == 200){
            return $url['response'];
//            header('Location: ' . $url['response']); 
        }
    
       /* $response = $this->oauth->getAuthorizeURL($token, $sign_in_with_kaixin);
        var_dump($response);
        
        return array(
            'httpcode' => $this->oauth->http_code,
            'response' => $response,
        );*/
        
    }
    
    /*
     * 获取用户基本信息
     */
    public function get_user_info() {
        $url = 'users/me';
        $param = array(
            'fields' => $fields,
        );
        $response = $this->oauth->get($url, $param);
        
        $userinfo['gender'] = $response->gender == 0 ? 'm' : 'f';
        $userinfo['nickname'] = $response->name;
        $userinfo['head_url'] = $response->logo50;
        $userinfo['uid'] = $response->uid;
        
        return $userinfo;
       
    }
    
    /*
     * (non-PHPdoc) @see Lib_openapi_abstract::sent_message()
     */
    public function sent_message() {
        // TODO Auto-generated method stub
    
    }

}
